package com.example.moblie203zlw20216061300601;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {
    boolean aBoolean=true;
    SharedPreferences sp;
    private TextView textView;
    private EditText edtTxtMainUserName;
    private EditText edtTxtMainPassword;
    private CheckBox chkBoxMainPSW;
    private CheckBox chkBoxMainName;
    private Button btnMainLogin;
    private Button btnMainReset;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        sp=getSharedPreferences("login",MODE_PRIVATE);
    }

    private void initView() {
        textView = (TextView) findViewById(R.id.textView);
        edtTxtMainUserName = (EditText) findViewById(R.id.edtTxtMainUserName);
        edtTxtMainPassword = (EditText) findViewById(R.id.edtTxtMainPassword);
        chkBoxMainPSW = (CheckBox) findViewById(R.id.chkBoxMainPSW);
        chkBoxMainName = (CheckBox) findViewById(R.id.chkBoxMainName);
        btnMainLogin = (Button) findViewById(R.id.btnMainLogin);
        btnMainReset = (Button) findViewById(R.id.btnMainReset);
    }

    @Override
    protected void onStart() {
        String name = sp.getString("用户","");
        String passopen=sp.getString("密码","");
        boolean user = sp.getBoolean("记住用户", false);
        boolean password = sp.getBoolean("记住密码", false);
        if (user){
            chkBoxMainName.setChecked(user);
            edtTxtMainUserName.setText(name);
        }
        if (password){
            chkBoxMainPSW.setChecked(password);
            edtTxtMainPassword.setText(passopen);
        }
        super.onStart();
    }

    public void denlu(View view) {
        String UserName=edtTxtMainUserName.getText().toString();
        String password=edtTxtMainPassword.getText().toString();
        String name = sp.getString("用户","");
        String pass=sp.getString("密码","");
        if (UserName.equals(name) & password.equals(pass)){
            Toast.makeText(this, "登录成功", Toast.LENGTH_SHORT).show();
        }
    }
    public void chongzhi(View view) {
        String UserName=edtTxtMainUserName.getText().toString();
        String password=edtTxtMainPassword.getText().toString();
        SharedPreferences.Editor edit = sp.edit();
        if (UserName!=null & password!=null){
            edit.putString("用户",UserName);
            edit.putString("密码",password);
            edit.commit();
            Toast.makeText(this, "设置成功", Toast.LENGTH_SHORT).show();
        }else {
            Toast.makeText(this, "账号或者密码为空", Toast.LENGTH_SHORT).show();
        }
    }

    public void xuanzhon(View view) {
        SharedPreferences.Editor edit = sp.edit();
                if (chkBoxMainPSW.isChecked()){
                    edit.putBoolean("记住用户",true);
                    edit.commit();
                }else{
                    edit.putBoolean("记住用户",false);
                    edit.commit();
                }

    }

    public void xuanzhon2(View view) {
        SharedPreferences.Editor edit = sp.edit();
        if (chkBoxMainPSW.isChecked()){
            edit.putBoolean("记住密码",true);
            edit.commit();
        }else {
            edit.putBoolean("记住密码",false);
            edit.commit();
        }
    }
}